<?php

namespace app\admin\controller;

use app\common\Constants;
use app\common\service\VerificationCodeService;
use think\App;
use think\exception\ValidateException;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Log;

class Tools extends Admin
{

    public function __construct(
        App                               $app,
        protected VerificationCodeService $verificationCodeService,
    )
    {
        parent::__construct($app);
    }


    /**
     * 发送短信验证码
     * @return \think\response\Json
     */
    public function sendSmsCode(): \think\response\Json
    {
        $mobile = $this->request->post('mobile');

        if (empty($mobile)) {
            throw new ValidateException(Constants::E_COMMON_DATA_VALIDATION_FAILED);
        }

        $code = $this->verificationCodeService->generateCode();
        $key = Constants::CACHE_VERIFICATION_CODE_KEY . $mobile;
        $this->verificationCodeService->cacheCode($key, $code);

        if (app()->isDebug()) {
            $data = ['mobile' => $mobile, 'code' => $code];
            trace($data, 'debug');
            return $this->success(['data' => $data]);
        } else {
            $this->verificationCodeService->send('sms', $mobile, $code);
            return $this->success();
        }
    }

    /**
     * 发送验证码
     *
     * @return \think\response\Json
     */
    public function sendEmailCode(): \think\response\Json
    {
        $email = $this->request->post('email');
        $username = $this->request->post('username', '用户');

        if (empty($email)) {
            throw new ValidateException(Constants::E_COMMON_DATA_VALIDATION_FAILED);
        }

        $code = $this->verificationCodeService->generateCode();
        $key = Constants::CACHE_VERIFICATION_CODE_KEY . $email;
        $this->verificationCodeService->cacheCode($key, $code);

        if (app()->isDebug()) {
            $data = ['mail' => $email, 'code' => $code];
            trace($data, 'debug');
            return $this->success(['data' => $data]);
        } else {
            $result = $this->verificationCodeService->send('email', $email, $code, ['username' => $username]);
            if ($result) {
                return $this->success();
            } else {
                return $this->error();
            }
        }
    }

    /**
     * 清理缓存
     *
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function cleanCache(): \think\response\Json
    {
        $post = $this->request->post();
        $cache_key = $post['cache_key'];
        if (in_array('cache', $cache_key)) {
            Cache::clear();
        }
        if (in_array('log', $cache_key)) {
            Db::name('admin_log')->delete(true);
        }
        if (in_array('runtime', $cache_key)) {
            $runtimePath = app()->getRuntimePath();
            $result = delete_dir_recursively($runtimePath, false);
            if (!$result) {
                throw new \Exception(Constants::E_COMMON_CLEAN_RUNTIME_DATA_FAILED);
            }
        }
        return $this->success(['message' => lang('responses.success')]);
    }
}